package com.rockcent.helper;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.utils.RBeanUtils;
import com.rockcent.mall.lib.constants.SysMessageConstants;
import com.rockcent.mall.lib.domain.SysMessage;
import com.rockcent.mall.lib.domain.enumclass.SysMessageType;
import com.rockcent.mall.lib.domain.enumclass.TerminalType;
import com.rockcent.mall.lib.dto.SysMessageDto;
import com.rockcent.service.SysMessageApiService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by zhangjh on 2017/5/25.
 */
public enum SysMessageHelper {

    SINGLETON;
    private final static String url = "http://127.0.0.1:8833";
    private final Logger log = LoggerFactory.getLogger(SysMessageHelper.class);


    /**
     * @param messageConstants
     * @param messageType
     * @param content
     * @return
     */
    public SysMessage createMsgBean(SysMessageConstants messageConstants, SysMessageType messageType, String content) {
        SysMessage message = new SysMessage();
        if (null == messageType) {
            messageType = SysMessageType.SYS;
        }
        if (null != messageConstants) {
            if (StringUtils.isBlank(content)) {
                message.setContent(messageConstants.getContent());
            } else {
                message.setContent(content);
            }
            message.setBackendUrl(messageConstants.getBackendUrl());
            message.setFrontendUrl(messageConstants.getFrontendUrl());
            message.setFrontendResourceUrl(messageConstants.getFrontendResourceUrl());
            message.setTerminalType(TerminalType.valueOf(messageConstants.getTerminalType()));
        }
        message.setType(messageType);
        return message;
    }

    /**
     * @param message
     * @param mallId
     * @param merchantId
     * @throws Exception
     */
    public void pushMsgByMallAndMerchantId(SysMessage message, Long mallId, Long merchantId) throws Exception {
//        log.info("======================>message：{},mallId：{},merchantId：{}", message, mallId, merchantId);
        Retrofit retrofit = RetrofitHelper.SINGLETON.getRetrofit(url);
        SysMessageApiService sysMessageSrv = retrofit.create(SysMessageApiService.class);
        SysMessageDto sysMessageDto = new SysMessageDto();
        RBeanUtils.copyProperties(message, sysMessageDto);
        sysMessageDto.setType(message.getType().toString());
        sysMessageDto.setUserId(null);
        sysMessageDto.setMallId(mallId);
        sysMessageDto.setMerchantId(merchantId);
        sysMessageDto.setType(message.getType().toString());
        sysMessageDto.setTerminalType(message.getTerminalType().toString());
//        log.info("======================>pushMsg sysMessageDto：{}", sysMessageDto);
        Response<JsonResult> resultResponse = sysMessageSrv.pushMsg(sysMessageDto).execute();
//        log.info("======================>pushMsg resultResponse：{}", resultResponse);
        if (null != resultResponse.errorBody()) {
//            log.info("======================>pushMsg errorBody-：{}", resultResponse.errorBody().string());
        } else {
//            log.info("======================>pushMsg body：{}", resultResponse.body());
        }
    }

    /**
     * 系统广播消息
     *
     * @param messageConstants
     * @param content
     * @param mallId
     * @throws Exception
     */
    public void pushSysMsg(SysMessageConstants messageConstants, String content, Long mallId, Long merchantId) throws Exception {
        pushMsgByMallAndMerchantId(createMsgBean(messageConstants, SysMessageType.SYS, content), mallId, merchantId);
    }

    /**
     * 运营广播消息
     *
     * @param messageConstants
     * @param content
     * @param mallId
     * @throws Exception
     */
    public void pushOpMsg(SysMessageConstants messageConstants, String content, Long mallId, Long merchantId) throws Exception {
        pushMsgByMallAndMerchantId(createMsgBean(messageConstants, SysMessageType.OP, content), mallId, merchantId);
    }

    /**
     * 财务广播消息
     *
     * @param messageConstants
     * @param content
     * @param mallId
     * @throws Exception
     */
    public void pushFinanceMsg(SysMessageConstants messageConstants, String content, Long mallId, Long merchantId) throws Exception {
        pushMsgByMallAndMerchantId(createMsgBean(messageConstants, SysMessageType.FINANCE, content), mallId, merchantId);
    }
}
